#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/1/25 23:01
# @Author  : Ramsey
# @Site    : zh
# @File    : __init__.py.py
# @Software: PyCharm
from spider_framework.http.request import Request


class Spider:
    def __init__(self):
        self.crawler_object = None
        if not hasattr(self, "start_urls"):
            self.start_urls = []

    @classmethod
    def create_instance(cls, crawler):
        crawler_object = cls()
        crawler_object.crawler = crawler
        return crawler_object

    def start_request(self):
        if self.start_urls:
            for url in self.start_urls:
                yield Request(url=url)
        else:
            if all([
                hasattr(self, "start_urls"),
                isinstance(getattr(self, "start_urls"), str)
            ]):
                yield self.start_urls

    def parse(self, response):
        raise NotImplementedError("parse method must be implemented")

    def __str__(self):
        return f"{self.__class__.__name__}爬虫"


if __name__ == "__main__":
    pass
